package com.solution.common.utils.sms;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.protocol.Protocol;

/**
 * @function  消息发送模板
 * @description 
 * @author 未知
 * @see [相关类/方法](可选)
 * @since 1.0
 */
public class MasSendMessageTemplate {

	// 短信服务器地址
	private static String SMS_SERVICE = "http://121.40.105.168:8080/sms";
	// 短信服务器接口
	private static String SEND_MSG_URL = SMS_SERVICE + "/smsInterface/sendMessage.do";
	// 发送短信系统名
	private static String SEND_SYSTEM = "UPROJECT+";
	/**
	 * 发送短信
	 * @作者：yaoyu
	 * @时间：2016年4月28日 下午10:35:56
	 * @param destAddr 手机号码
	 * @param content 发送内容
	 * @param msgType 发送类型
	 * @param configId
	 * @return
	 */
	public static JSONObject sendMsg(String destAddr, String content){
		JSONObject resultJson = new JSONObject();
		try {
			JSONObject requestData = new JSONObject();
			//发送短信内容
			requestData.put("TEL", destAddr);
			requestData.put("MSG_CONTENT", content);
			requestData.put("SYSTEM", SEND_SYSTEM);
			requestData.put("MSG_TYPE", "checkCode");
			//短信发送逻辑处理
			String returnData = doPost(SEND_MSG_URL, "requestData", requestData.toJSONString());
			resultJson = MasSendMessageTemplate.checkReturnData(returnData);
			System.out.println(requestData.toJSONString() + "==->" + resultJson.toJSONString());
		} catch (Exception e) {
			resultJson.put("code", "-1");
			resultJson.put("msg", "调用发送短信接口异常:" + e.getMessage());
			e.printStackTrace();
			return resultJson;
		}
		return resultJson;
	}
	
	/**
	 * @Description: 解析请求短信接口发送方法返回的json数据
	 * @author yuzj  
	 * @created 2015-11-20 上午10:03:21
	 * @param returnData	短信接口发送方法返回的json数据
	 * @return
	 */
	private static JSONObject checkReturnData(String returnData){
		JSONObject resultJson = new JSONObject();
		if(isNull(returnData)){
			resultJson.put("code", "-1");
			resultJson.put("msg", "调用发送短信接口出错");
			return resultJson;
		}
		try {
			resultJson = JSONObject.parseObject(returnData);
			if(isNull(resultJson.getString("code"))){
				resultJson.put("code", "-1");
				resultJson.put("msg", "调用发送短信接口出错");
				return resultJson;
			}
		} catch (Exception e) {
			resultJson.put("code", "-1");
			resultJson.put("msg", "调用发送短信接口异常："+e.getMessage());
			e.printStackTrace();
			return resultJson;
		}
		return resultJson;
	}
	
	/**
	 * @Description: 封装工具判断方法
	 * @author chenling  
	 * @created 2017-5-8 上午10:13:21
	 * @param str	参数
	 * @return
	 */
     public static boolean isNull(String str)
		  {
		    if ((str == null) || (str.trim().equals(""))) {
		      return true;
		    }
		    return false;
		  }
    /**
	 * @Description: 解析请求短信接口post 封装方法
	 * @author chenling  
	 * @created 2017-5-8 上午10:13:21
	 * @param url	请求地址
	 * @param name	封装参数名字
	 * @param responeJsonStr  请求参数封装
	 * @return
	 */
     public static String doPost(String url, String name, String responeJsonStr)
		    throws IOException, HttpException
		  {
		    Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443);
		    Protocol.registerProtocol("https", myhttps);

		    HttpClient client = new HttpClient();
		    PostMethod post = new PostMethod(url);
		    post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		    if (name!=null && !name.equals("") ) {
		      NameValuePair[] param = { new NameValuePair(name, responeJsonStr) };
		      post.setRequestBody(param);
		    } else {
		      post.setRequestBody(responeJsonStr);
		    }
		    post.getParams().setContentCharset("utf-8");
		    String respStr = "";
		    client.executeMethod(post);
		    respStr = post.getResponseBodyAsString();
		    return respStr;
		  }
  
  
	 public static void main(String[] args) {
		  MasSendMessageTemplate.sendMsg("13951843079", "测试");
	  }
  
}